package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private  String app_id;

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC7RTfoucCRElWroVcJbax90f2CxSTYYJ4STUY10c51kTSfCdoouA1synwSICRROUYA9pfue2xIqZGO6Ld4KowhdyZi+KjluE9dWL8r+7/nqHXEXj01pUv9qS9kVq6uvsSnwQU9tVj2Jp5ZUIseSpVDAzJQHvwMytZN/Uf1/tgIcrcrNYJTCO6+/bDl9cBPTVeUaS4C0dyntl5Pel5R31+D5t1mJFVFiEjHj+Yfkx3rqMHBFAklRE5Q34B3aj2w+E5qjn9J2LFRqYNiV+gxs/hHxeUVnMDpDMEnkERD/zrmbomQJOU18McCwtL5C79dMJii3TUkxCnlIT62Ck40IVbvAgMBAAECggEAdfq9pligAMVUQ7R5aO83ZG0PEXuyIr971nvoZ6+uKlM4m5lRX6XUj11hnOmy95jliWXRgeUv6vcpRKdILuRfNBYkkOWnQlJDbcOjbMpFtGRlDTlxxoJExaH4wXX6xwxYSHdKHf5J9YmO6SXc/1BtiLJS2DctA8ImESwoUcojv+2w0eCWMBalR/8RKDu8niyCDEC21DVERFeNHVVQRRDyEAdj6xtaarXyIpK9KWBf30k69ckvHTjpHe59wHF1xq6nyfEvfxZftDiyFFEYWa1e3PU6PeTl+s76e7/oIKq4jADtqeIR161Ay0PQoDWsd53IPiDGNB40MZHxIPQTaqF0gQKBgQDskYAm/Pu9SBxAurjdCEw3E4k/x6c377LLJutiAHb0JD9uOhvingWk1SDAehPxKlv19EHHdrTZqqiPMJkuNuljFPLwlY07SoqbKw7YCkj4nu4Yxlvb6SxbZXD+nMCtq4N+3dpCotLLc8JfpJSORV7RwDCygVPzl5CmyQfmVUPZhwKBgQDKpxerSqmbbKKu+GoLvcvNdrBkWrz3/e4jLLXKNFVHNQlGrj6t16wgvpGsGBgxEpeVSu996Nfvhcbd8CpbRNtK0OoFJ2+hBHCQVp1b4Vfl82uR268v5svg9GeRpCa8HNmFi42syDKdT2HlV4B889ftsz9Oxe8+MPSgJFV2nFFRWQKBgFZJO9T8sVV419VERelY/XOZTJZW3EcqxqwSpgg44yppjjlGOKWPvb9BfZC29k8ghCpRzSxTv+MkvBZ+Q2D35midW38lDT5zEdtG+UMTfUoxreabcyXjBqrPdUdpJV4zEONVSCbF/Nz4fA0mmojKBKu8zgZN5bETt08eRCNfNkBBAoGAPV+tg2rxQuj7vBHvy+6HMoqnkT0JzfFRlcWMqNiAmzgsvwM2aF/Q+Xm9lGkXqoT+82/maDOEcj4JpF1/9nQ8FcR/m1/+Jr25eX1B2vm8LSAzi9EElXVIebCLTUhxkrcKCa/iEFnxOkM4WsTNXQpccti+a5T2h9c7vuRCHdHprsECgYABpC3ws3tVSKUsS3QR59kZJwvlQO3qRPkF0KZGIQ36oi0LnQyZJ5tbXU6aeXRK7Np5sAews5aeTlZEY94Kmk9OgIIDKhSVrpU6GckvGFUIe8nHXxOL0Dvr+4ST9utIw/qQPJ/DggSTPc5TyJ783dWAXFSC3eDBxy0ZK4cK97kOHA==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu1U/9qwNItAkJLjCUm9NObriuqiprNMf4+1luEVtxpMOVEe7c0xlIqZknfJz96yN48zh4/G3jsrX8N5Oua/HYX1c1eAK/DNOLdXPKrK+PiL1kX3HxlBr2tQ/BX6oaYGMwBLpvyU4bXT4V5omkq9elHUAwBSGk/TfQvnkLkVAKc8d8Jm/jGjBsZRqNmuiigTIDp5VQeCdRNAwlrScvIIMbmIaRux9gU8QmMUWE7BRLD8uYTTY+IcCX8ZScoqWOdVyjqOiB05ziiZxjcDHE81CUAI/81kPBHljo+EBwgHgSu2FROsHFwcEHzsvAdLcyzE/xXQfHpaoie12RVxy7PQMZwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://nike1.cn.utools.club/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    //支付超时时间
    private String timeout = "30m";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"" + timeout + "\","//自动收单 订单创建后超过这个时间不能支付  也可以主动调用
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
